<template>
  <div id="box">
    <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item,index in banners" :key="index">
        <img :src="item.imageUrl" alt="" class="img1">
      </el-carousel-item>
    </el-carousel>
    <h3>推荐歌单</h3>
    <div class="box">
      <div v-for="o in personalized" :key="o.id" class="lists" @click="goDetail(o.id)">
        <i class="el-icon-video-play"></i>
        <img :src="o.picUrl" class="image" />
        <div class="p">
          <p>{{o.name}}</p>
        </div>
      </div>
    </div>
    <h3 style="margin: 100px 0 20px 0;">最新音乐</h3>
    <div class="items">
      <div v-for="s in songs" :key="s.id" class="item">
        <div class="img-wrap">
          <img :src="s.picUrl" alt=""/>
          <!-- 点击播放音乐 -->
          <span class="el-icon-video-play" @click="paly(s.id)"></span>
        </div>
        <div class="song-wrap">
          <p class="p2" style="font-weight: bold;font-size: 16px;">{{s.song.name}}</p>
          <span class="span2">{{s.song.artists[0].name}}</span>
        </div>
      </div>
    </div>
    <h3 style="margin: 40px 0 20px 0;">推荐MV</h3>
    <div class="mv">
      <div v-for="m in mvs" :key="m.id" class="items" @click="goPlay(m.id)">
        <img :src="m.picUrl" alt="" class="cover">
        <span class="count"><i class="el-icon-video-play"></i>{{m.playCount}}</span>
        <div>
          <p>{{m.name}}</p>
          <span class="singer">{{m.artistName}}</span>
        </div>
      </div>
    </div>
    
  </div>
</template>

<script>
export default {
  name: 'DiscoveryPage',
  data(){
    return{
      banners: [],
      personalized: [],
      songs: [],
      mvs: []
    }
  },
  created(){
    // 调用轮播图接口
    this.$axios({
      url: '/banner',
      method: 'get',
      type: 0
    }).then(res=>{
      // console.log(res)
      this.banners = res.data.banners
    })
    // 推荐歌单接口
    this.$axios({
      url: '/personalized',
      method: 'get',
      params: {limit: 10}
    }).then(res=>{
      // console.log(res)
      this.personalized = res.data.result
    })
    // 最新音乐接口
    this.$axios({
      url: '/personalized/newsong',
      method: 'get',
    }).then(res=>{
      // console.log(res.data.result)
      this.songs = res.data.result
    })
    // 最新mv接口
    this.$axios({
      url: '/personalized/mv',
      method: 'get'
    }).then(res=>{
      // console.log(res.data.result)
      this.mvs = res.data.result
    })
  },
  methods:{
    paly(id){
      // console.log(id)
      this.$axios({
        url: '/song/url',
        method: 'get',
        params:{id}
      }).then(res=>{
        console.log(res)
        let mUrl = res.data.data[0].url
        // console.log(mUrl)
        // 通过自定义数据传入数据
        this.$bus.$emit('music',mUrl)
      })
    },
    goDetail(id){
      this.$router.push({
        name: 'playlist',
        query: { q: id }
      })
    },
    goPlay(id){
      this.$router.push({
        name: 'mvpage',
        query: {
          q: id
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
#box{
  width: 100%;
  height: 100%;
  .img1{
    width: 100%;
    object-fit: cover;
    border-radius: 12px;
  }
}

.box{
  width: 100%;
  height: 50vh;
  display: flex;
  justify-content: space-between;
  flex-flow: row wrap;
}
.lists{
  position: relative;
  width: 15%;
  overflow: hidden;
  padding: 20px;
  cursor: pointer;
  .el-icon-video-play{
    opacity: 0;
    position: absolute;
    bottom: 30%;
    right: 10%;
    font-size: 40px;
    color: #dd6d60;
    background-color: #ccc;
    border-radius: 50%;
  }
  .el-icon-video-play:hover{
    opacity: 1;
    position: absolute;
    bottom: 20%;
    right: 10%;
    font-size: 40px;
    color: #dd6d60;
    background-color: #ccc;
    border-radius: 50%;
  }
  .p{
    width: 95%;
    overflow: hidden;
    text-overflow: ellipsis;
    p{
      display: inline;
      white-space: nowrap;
      font-size: 12px;
      padding: 10px;
    }
  }
}
.image {
    width: 100%;
    object-fit: cover;
    display: block;
  }
h3{
  display: block;
  margin-left: 12px;
}
.items {
  height: 500px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  padding-left: 15px;
  .item:hover {
    background-color: #f5f5f5;
  }
  .item {
    height: 100px;
    width: 50%;
    display: flex;
    align-items: center;
    .index {
      margin-right: 15px;
      font-size: 15px;
    }
    .img-wrap:hover .el-icon-video-play {
      opacity: 1;
    }
    .img-wrap {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      img {
        width: 80px;
        height: 80px;
      }
      .el-icon-video-play {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 30px;
        height: 30px;
        color: #dd6d60;
        text-align: center;
        line-height: 30px;
        border-radius: 50%;
        font-size: 14px;
        background: rgba(255, 255, 255, 0.8);
        opacity: 0;
      }
      .el-icon-video-play::before {
        transform: translateX(3px);
      }
    }
    .song-wrap {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      height: 100%;
      padding: 10px;
      font-size: 12px;
      .singer {
        font-size: 14px;
        color: gray;
      }
    }
  }
}
.mv{
  width: 100%;
  display: flex;
  justify-content: space-around;
  flex-flow: row wrap;
  .items{
    position: relative;
    width: 20%;
    cursor: pointer;
    .cover{
      width: 80%;
      height: 20%;
      border-radius: 10px;
    }
    .count{
      position: absolute;
      top: 0;
      right: 25%;
      color: white;
      font-size: 12px;
    }
    .singer{
      font-size: 12px;
    }
    p{
      font-weight: bold;
    }
  }
  
}
</style>